import { Init, Provide, Inject } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { CollectionInfoEntity } from '../entity/info';
import { MemberUserInfoEntity } from '../../member/entity/info';

/**
 * collection模块-收藏信息
 */
@Provide()
export class CollectionInfoService extends BaseService {
  @InjectEntityModel(CollectionInfoEntity)
  collectionInfoEntity: Repository<CollectionInfoEntity>;

  @InjectEntityModel(MemberUserInfoEntity)
  memberUserInfoEntity: Repository<MemberUserInfoEntity>;

  @Init()
  async init() {
    await super.init();
    this.setEntity(this.collectionInfoEntity);
  }


  @Inject()
  ctx;

  // 获取用户收藏列表  
  async updateCollection(queryData: any) {
    console.log(queryData, 'queryData')
    let userId = this.ctx.header.token
    if(!userId){
      throw new Error('请登陆后操作');
    }
    let data = await this.collectionInfoEntity.findOneBy({
      userId,
      collectionId: queryData.id,
    })
    if (data) {
      let res = await this.collectionInfoEntity.update({
        userId,
        collectionId: queryData.id,
      }, {
        status: queryData.isCollect ? 1 : 0,
      })
      return res
    } else {
      let params = {
        collectionId: queryData.id,
        userId,
        contentId: "",
        type: 2,
        status: queryData.isCollect ? 1 : 0,
        remark: "",
        createTime: new Date(),
        updateTime: new Date(),
        operator: userId,
        operateTime: new Date(),
      }
      let res = await this.collectionInfoEntity.save(params)
      return res
    }
  }

  // 获取我的收藏
  async getCollect(queryData: any) {
    let userId = this.ctx.header.token
    let sql = "select * from collection_info where status = 1 and userId = " + userId + " ORDER BY createTime DESC"
    let res = await this.sqlRenderPage(sql, queryData, false);
    let memberItem: any = await this.memberUserInfoEntity.findOneBy({ id: userId })
    // console.log(memberItem, 'memberItem')
    if (res.list.length) {
      let resSql = res.list.map(o => Number(o.collectionId)).join(',')
      let photoSql = "select * from anchor_info where id IN ( " + resSql + " ) ORDER BY createTime DESC"
      let data: any = await this.nativeQuery(photoSql);
      data.forEach(element => {
        element.isCollect = true
      });
      res.list = data
    }

    let anchor = res.list.map(o => Number(o.anchor)).join(',')
    let anchorList = []
    if (anchor.length) {
      let anchorSql = "select * from demo_user_info where id IN ( " + anchor + " )"
      anchorList = await this.nativeQuery(anchorSql);
    }

    let isVip = memberItem.level > 0 && memberItem.expireTime > new Date()
    res.list.forEach(o => {
      let item = anchorList.find(o1 => o1.id == o.anchor)
      o.anchor = item
      o.isVipCurrentUser = !isVip
      o.isVip = o.permission == 0 || (o.permission == 1 && isVip) // 不是vip
      o.isVipExpire = memberItem.expireTime < new Date()
      o.imageNum = JSON.parse(JSON.stringify(o.image)).length
      o.image = o.isVip ? o.image : o.image.splice(0, 3)
    })
    return res
  }

}
